//----------------------------头文件依赖----------------------------//
#include "LCD_TL021WVC02.h"
#include "dev_systick.h"

/* TFT-LCD 模组名 : TL021WVC02 (冠显光电), 屏内 COG 芯片型号 : ST7701S */

//----------------------------本地宏----------------------------//
#if (SWM34S_LCM_PCBV == SWM34SRE_PIN64_A001)// SWM34SRET6_Pin64

#define LCD_GPIO_CS 			GPIOD
#define LCD_PIN_CS 				PIN9

#define LCD_GPIO_SCK 			GPION
#define LCD_PIN_SCK 			PIN5

#define LCD_GPIO_SDA 			GPION
#define LCD_PIN_SDA 			PIN4

#else // SWM34SVET6_Pin100

#define LCD_GPIO_CS 			GPIOB
#define LCD_PIN_CS 				PIN0

#define LCD_GPIO_SCK 			GPION
#define LCD_PIN_SCK 			PIN5

#define LCD_GPIO_SDA 			GPION
#define LCD_PIN_SDA 			PIN4

#endif

//操作宏
#if 	0 //库函数

#define PIN_HIGH_RST() 		GPIO_AtomicSetBit(LCD_GPIO_RST, LCD_PIN_RST) //GPIO_SetBit(LCD_GPIO_RST, LCD_PIN_RST)
#define PIN_LOW_RST() 		GPIO_AtomicClrBit(LCD_GPIO_RST, LCD_PIN_RST) //GPIO_ClrBit(LCD_GPIO_RST, LCD_PIN_RST)

#define PIN_HIGH_CS() 		GPIO_AtomicSetBit(LCD_GPIO_CS, LCD_PIN_CS) //GPIO_SetBit(LCD_GPIO_CS, LCD_PIN_CS)
#define PIN_LOW_CS() 		GPIO_AtomicClrBit(LCD_GPIO_CS, LCD_PIN_CS) //GPIO_ClrBit(LCD_GPIO_CS, LCD_PIN_CS)

#define PIN_HIGH_SCK() 		GPIO_AtomicSetBit(LCD_GPIO_SCK, LCD_PIN_SCK) //GPIO_SetBit(LCD_GPIO_SCK, LCD_PIN_SCK)
#define PIN_LOW_SCK() 		GPIO_AtomicClrBit(LCD_GPIO_SCK, LCD_PIN_SCK) //GPIO_ClrBit(LCD_GPIO_SCK, LCD_PIN_SCK)

#define PIN_HIGH_SDA() 		GPIO_AtomicSetBit(LCD_GPIO_SDA, LCD_PIN_SDA) //GPIO_SetBit(LCD_GPIO_SDA, LCD_PIN_SDA)
#define PIN_LOW_SDA() 		GPIO_AtomicClrBit(LCD_GPIO_SDA, LCD_PIN_SDA) //GPIO_ClrBit(LCD_GPIO_SDA, LCD_PIN_SDA)

#else //寄存器

#define PIN_HIGH_RST() 		(*(&LCD_GPIO_RST->DATAPIN0 + LCD_PIN_RST) = 1) //LCD_GPIO_RST->ODR |= (0x01 << LCD_PIN_RST)
#define PIN_LOW_RST() 		(*(&LCD_GPIO_RST->DATAPIN0 + LCD_PIN_RST) = 0) //LCD_GPIO_RST->ODR &= ~(0x01 << LCD_PIN_RST)

#define PIN_HIGH_CS() 		(*(&LCD_GPIO_CS->DATAPIN0 + LCD_PIN_CS) = 1) //LCD_GPIO_CS->ODR |= (0x01 << LCD_PIN_CS)
#define PIN_LOW_CS() 		(*(&LCD_GPIO_CS->DATAPIN0 + LCD_PIN_CS) = 0) //LCD_GPIO_CS->ODR &= ~(0x01 << LCD_PIN_CS)

#define PIN_HIGH_SCK() 		(*(&LCD_GPIO_SCK->DATAPIN0 + LCD_PIN_SCK) = 1) //LCD_GPIO_SCK->ODR |= (0x01 << LCD_PIN_SCK)
#define PIN_LOW_SCK() 		(*(&LCD_GPIO_SCK->DATAPIN0 + LCD_PIN_SCK) = 0) //LCD_GPIO_SCK->ODR &= ~(0x01 << LCD_PIN_SCK)

#define PIN_HIGH_SDA() 		(*(&LCD_GPIO_SDA->DATAPIN0 + LCD_PIN_SDA) = 1) //LCD_GPIO_SDA->ODR |= (0x01 << LCD_PIN_SDA)
#define PIN_LOW_SDA() 		(*(&LCD_GPIO_SDA->DATAPIN0 + LCD_PIN_SDA) = 0) //LCD_GPIO_SDA->ODR &= ~(0x01 << LCD_PIN_SDA)

#endif

//----------------------------本地函数定义----------------------------//
static inline void spi_delay_time(void)
{
	for (int us = 0; us < 100; us++)
		__NOP();
}

static void spi_send_data(unsigned char i)
{
	for (unsigned char n = 0; n < 8; n++)
	{
		if (i & 0x80)
		{
			PIN_HIGH_SDA();
		}
		else
		{
			PIN_LOW_SDA();
		}
		i <<= 1;
		spi_delay_time();
		PIN_LOW_SCK();
		spi_delay_time();
		PIN_HIGH_SCK();
		spi_delay_time();
	}
}

static void spi_write_cmd(unsigned char i)
{
	PIN_LOW_CS();
	spi_delay_time();
	PIN_LOW_SDA();
	spi_delay_time();
	PIN_LOW_SCK();
	spi_delay_time();

	PIN_HIGH_SCK();
	spi_delay_time();
	spi_send_data(i);
	spi_delay_time();
	PIN_HIGH_CS();
	spi_delay_time();
}

static void spi_write_data(unsigned char i)
{
	PIN_LOW_CS();
	spi_delay_time();
	PIN_HIGH_SDA();
	spi_delay_time();
	PIN_LOW_SCK();
	spi_delay_time();

	PIN_HIGH_SCK();
	spi_delay_time();
	spi_send_data(i);
	spi_delay_time();
	PIN_HIGH_CS();
	spi_delay_time();
}

static void spi_port_init(void)
{
	GPIO_Init(LCD_GPIO_CS, LCD_PIN_CS, 1, 1, 0, 0);
	GPIO_Init(LCD_GPIO_SCK, LCD_PIN_SCK, 1, 1, 0, 0);
	GPIO_Init(LCD_GPIO_SDA, LCD_PIN_SDA, 1, 1, 0, 0);
	GPIO_Init(LCD_GPIO_RST, LCD_PIN_RST, 1, 1, 0, 0);
	PIN_HIGH_CS();
	PIN_HIGH_SCK();
	PIN_HIGH_SDA();

	PIN_HIGH_RST(); //复位
	spi_delay_time();//swm_delay_ms(1);
	PIN_LOW_RST();
	swm_delay_ms(1);
	PIN_HIGH_RST();
}

//----------------------------对外函数定义----------------------------//
void lcd_spi_init_TL021WVC02(void)
{
	spi_port_init();

	PIN_LOW_CS();
	spi_delay_time();
	PIN_HIGH_SDA();
	swm_delay_ms(8);

	PIN_LOW_SDA();
	swm_delay_ms(50);
	PIN_HIGH_SDA();
	swm_delay_ms(50);

	spi_write_cmd(0x11); //BOE3.97IPS
	swm_delay_ms(150);

#if 1 //缩进便于观看
    spi_write_cmd(0xFF);
    spi_write_data(0x77);
    spi_write_data(0x01);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x10);

    spi_write_cmd(0xC0);
    spi_write_data(0x3B);
    spi_write_data(0x00);

    spi_write_cmd(0xC1);
    spi_write_data(0x0B); //VBP
    spi_write_data(0x02);

    spi_write_cmd(0xC2);
    spi_write_data(0x00);
    spi_write_data(0x02);

    spi_write_cmd(0xCC);
    spi_write_data(0x10);

    spi_write_cmd(0xCD);
    spi_write_data(0x08);

    spi_write_cmd(0xB0); //Positive Voltage Gamma Control
    spi_write_data(0x02);
    spi_write_data(0x13);
    spi_write_data(0x1B);
    spi_write_data(0x0D);
    spi_write_data(0x10);
    spi_write_data(0x05);
    spi_write_data(0x08);
    spi_write_data(0x07);
    spi_write_data(0x07);
    spi_write_data(0x24);
    spi_write_data(0x04);
    spi_write_data(0x11);
    spi_write_data(0x0E);
    spi_write_data(0x2C);
    spi_write_data(0x33);
    spi_write_data(0x1D);

    spi_write_cmd(0xB1); //Negative Voltage Gamma Control
    spi_write_data(0x05);
    spi_write_data(0x13);
    spi_write_data(0x1B);
    spi_write_data(0x0D);
    spi_write_data(0x11);
    spi_write_data(0x05);
    spi_write_data(0x08);
    spi_write_data(0x07);
    spi_write_data(0x07);
    spi_write_data(0x24);
    spi_write_data(0x04);
    spi_write_data(0x11);
    spi_write_data(0x0E);
    spi_write_data(0x2C);
    spi_write_data(0x33);
    spi_write_data(0x1D);

    spi_write_cmd(0xFF);
    spi_write_data(0x77);
    spi_write_data(0x01);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x11);

    spi_write_cmd(0xB0);
    spi_write_data(0x5d); //5d

    spi_write_cmd(0xB1); //VCOM amplitude setting
    spi_write_data(0x43); //43

    spi_write_cmd(0xB2); //VGH Voltage setting
    spi_write_data(0x81); //12V

    spi_write_cmd(0xB3);
    spi_write_data(0x80);

    spi_write_cmd(0xB5); //VGL Voltage setting
    spi_write_data(0x43); //-8.3V

    spi_write_cmd(0xB7);
    spi_write_data(0x85);

    spi_write_cmd(0xB8);
    spi_write_data(0x20);

    spi_write_cmd(0xC1);
    spi_write_data(0x78);

    spi_write_cmd(0xC2);
    spi_write_data(0x78);

    spi_write_cmd(0xD0);
    spi_write_data(0x88);

    spi_write_cmd(0xE0);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x02);

    spi_write_cmd(0xE1);
    spi_write_data(0x03);
    spi_write_data(0xA0);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x04);
    spi_write_data(0xA0);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x20);
    spi_write_data(0x20);

    spi_write_cmd(0xE2);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);

    spi_write_cmd(0xE3);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x11);
    spi_write_data(0x00);

    spi_write_cmd(0xE4);
    spi_write_data(0x22);
    spi_write_data(0x00);

    spi_write_cmd(0xE5);
    spi_write_data(0x05);
    spi_write_data(0xEC);
    spi_write_data(0xA0);
    spi_write_data(0xA0);
    spi_write_data(0x07);
    spi_write_data(0xEE);
    spi_write_data(0xA0);
    spi_write_data(0xA0);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);

    spi_write_cmd(0xE6);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x11);
    spi_write_data(0x00);

    spi_write_cmd(0xE7);
    spi_write_data(0x22);
    spi_write_data(0x00);

    spi_write_cmd(0xE8);
    spi_write_data(0x06);
    spi_write_data(0xED);
    spi_write_data(0xA0);
    spi_write_data(0xA0);
    spi_write_data(0x08);
    spi_write_data(0xEF);
    spi_write_data(0xA0);
    spi_write_data(0xA0);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);

    spi_write_cmd(0xEB);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x40);
    spi_write_data(0x40);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);

    spi_write_cmd(0xED);
    spi_write_data(0xFF);
    spi_write_data(0xFF);
    spi_write_data(0xFF);
    spi_write_data(0xBA);
    spi_write_data(0x0A);
    spi_write_data(0xBF);
    spi_write_data(0x45);
    spi_write_data(0xFF);
    spi_write_data(0xFF);
    spi_write_data(0x54);
    spi_write_data(0xFB);
    spi_write_data(0xA0);
    spi_write_data(0xAB);
    spi_write_data(0xFF);
    spi_write_data(0xFF);
    spi_write_data(0xFF);

    spi_write_cmd(0xEF);
    spi_write_data(0x10);
    spi_write_data(0x0D);
    spi_write_data(0x04);
    spi_write_data(0x08);
    spi_write_data(0x3F);
    spi_write_data(0x1F);

    spi_write_cmd(0xFF);
    spi_write_data(0x77);
    spi_write_data(0x01);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x13);

    spi_write_cmd(0xEF);
    spi_write_data(0x08);

    spi_write_cmd(0xFF);
    spi_write_data(0x77);
    spi_write_data(0x01);
    spi_write_data(0x00);
    spi_write_data(0x00);
    spi_write_data(0x00);

    spi_write_cmd(0x11);
#endif

    swm_delay_ms(120);

    spi_write_cmd(0x29);

    spi_write_cmd(0x36);
    spi_write_data(0x00);

    spi_write_cmd(0x3A);
    spi_write_data(0x60);
}
